package com.lmx.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lmx.project.common.BaseResponse;
import com.lmx.project.common.ErrorCode;
import com.lmx.project.common.ResultUtils;
import com.lmx.project.exception.BusinessException;
import com.lmx.project.model.dto.conversion.ConversionAddRequest;
import com.lmx.project.model.dto.conversion.ConversionUpdateRequest;
import com.lmx.project.model.entity.Chat;
import com.lmx.project.model.entity.Conversion;
import com.lmx.project.service.ChatService;
import com.lmx.project.service.ConversionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("conversion")
public class ConversionController {
    @Resource
    private ConversionService conversionService;

    @Resource
    private ChatService chatService;

    /**
     * 创建对话
     */
    @PostMapping
    public BaseResponse<Long> addConversion(ConversionAddRequest conversionAddRequest) {
        if (conversionAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        if (!StringUtils.isNotBlank(conversionAddRequest.getTitle())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入对话标题");
        }

        if (!StringUtils.isNotBlank(conversionAddRequest.getInstructions())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入角色指令");
        }

        Conversion conversion = new Conversion();
        BeanUtils.copyProperties(conversionAddRequest, conversion);

        boolean save = conversionService.save(conversion);

        return ResultUtils.success(conversion.getId());
    }

    /**
     * 删除对话
     */
    @DeleteMapping
    public BaseResponse<Boolean> deleteConversion(Long id) {


        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该对话不存在");
        }

        boolean save = conversionService.removeById(id);

        return ResultUtils.success(save);
    }

    /**
     * 修改对话
     */
    @PutMapping
    public BaseResponse<Boolean> UpdateConversion(ConversionUpdateRequest conversionUpdateRequest) {
        if (conversionUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        if (conversionUpdateRequest.getId() == null || conversionUpdateRequest.getId() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该对话不存在");
        }

        Conversion conversion = new Conversion();
        BeanUtils.copyProperties(conversionUpdateRequest, conversion);

        boolean save = conversionService.updateById(conversion);

        return ResultUtils.success(save);
    }


    /**
     * 查询所有对话
     */
    @GetMapping
    public BaseResponse<List<Conversion>> getConversionBylist() {


        List<Conversion> list = conversionService.list();
        return ResultUtils.success(list);
    }


    /**
     * 清空该对话下的所有对话
     */
    @DeleteMapping("list")
    public BaseResponse<Boolean> deleteChatById(Long id) {
        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该对话不存在");
        }
        Conversion byId = conversionService.getById(id);
        if (byId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该对话不存在");
        }

        LambdaQueryWrapper<Chat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chat::getConversionid, id);
        boolean remove = chatService.remove(queryWrapper);
        return ResultUtils.success(remove);
    }


}
